<?php

namespace App\Controllers\Admin;
use Pub;
use Sentry,User,DB,Code,Machine,Material;
use App;
use BaseController,View,Input,Form,Order,Customer,Redirect,Cache,Wechat;
class AccountsController extends \BaseController {

	/**
	 * Display a listing of the resource.
	 * GET /admin/codes
	 *
	 * @return Response
	 */
    public function index()
    {
        //
    }

    public function materials()
    {
        //

        $pub = Pub::find(1);

        $PATH = $pub->path;
        $METHOD = 'accounts';
        $ACTION = 'materials';
        $materials = Material::orderBy('created_at', 'DESC')->paginate(20);
        return \View::make('admin.accounts.materials',compact('METHOD','ACTION','materials','PATH'));
    }

    public function materials_edit()
    {
        //

        $pub = Pub::find(1);

        $PATH = $pub->path;
        $METHOD = 'accounts';
        $ACTION = 'materials';
        $id = Input::get('id');
        $material = Material::find($id);
        return \View::make('admin.accounts.materials_edit',compact('METHOD','ACTION','material','PATH'));
    }

    public function materials_create()
    {
        //

        $pub = Pub::find(1);

        $PATH = $pub->path;
        $METHOD = 'accounts';
        $ACTION = 'materials';
//        $machines = Machine::orderBy('created_at', 'DESC')->paginate(20);
        return \View::make('admin.accounts.materials_create',compact('METHOD','ACTION','machines','PATH'));
    }
    public function materials_store()
    {
        //
        require app_path().'/lib/qiniu/rs.php';
        require app_path().'/lib/qiniu/io.php';
        $material = new Material();
        $material->title = Input::get('title');
        $material->type = Input::get('type');
        $material->key = Input::get('key');
        $material->author = Input::get('author');
        $material->points = Input::get('points');
        $material->dec = Input::get('dec');
        $material->url = Input::get('url');
        $material->cart = Input::get('cart');
        $material->content = Input::get('editorValue');
        $material->cover = $this->uploadfile($_FILES["cover"]["name"],$_FILES["cover"]["tmp_name"]);
        $material->save();
        return Redirect::route('admin.accounts.materials');
    }



    public function menus()
    {
        return Redirect::route('admin.menus.index');
    }
    public function menus_create()
    {
        //
    }

	/**
	 * Show the form for creating a new resource.
	 * GET /admin/codes/create
	 *
	 * @return Response
	 */
	public function create()
	{
		//
	}

	/**
	 * Store a newly created resource in storage.
	 * POST /admin/codes
	 *
	 * @return Response
	 */
	public function store()
	{
		//
	}

	/**
	 * Display the specified resource.
	 * GET /admin/codes/{id}
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function show($id)
	{
		//
	}

	/**
	 * Show the form for editing the specified resource.
	 * GET /admin/codes/{id}/edit
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function edit($id)
	{
		//
	}

	/**
	 * Update the specified resource in storage.
	 * PUT /admin/codes/{id}
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function update($id)
	{
		//
	}

    public function materials_update(){
        $id = Input::get('id');
        $material = Material::find($id);
        $material->title = Input::get('title');
        $material->type = Input::get('type');
        $material->key = Input::get('key');
        $material->author = Input::get('author');
        $material->points = Input::get('points');
        $material->dec = Input::get('dec');
        $material->url = Input::get('url');
        $material->cart = Input::get('cart');
        $material->content = Input::get('editorValue');
        if($_FILES['cover']["error"] == UPLOAD_ERR_OK) {
            require app_path().'/lib/qiniu/rs.php';
            require app_path().'/lib/qiniu/io.php';
            $material->cover = $this->uploadfile($_FILES["cover"]["name"], $_FILES["cover"]["tmp_name"]);
        }
        $material->save();
        return Redirect::route('admin.accounts.materials');
    }


    public function materials_deleteit(){

        $id = Input::get('delete_id');
        $material = Material::find($id);

        $material->delete();

        return Redirect::route('admin.accounts.materials');
    }
	/**
	 * Remove the specified resource from storage.
	 * DELETE /admin/codes/{id}
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function destroy($id)
	{
		//
	}
    public function uploadfile($name,$tmp){
        $type=array("jpg","gif","bmp","jpeg","png");//设置允许上传文件的类型

        if(!in_array(strtolower($this->fileext($name)),$type))
        {
            $text=implode(",",$type);
            echo "您只能上传以下类型文件: ",$text,"<br>";
        }
        //生成目标文件的文件名
        else{
            $filename=explode(".",$name);
            $filename[0]=$this->random(10); //设置随机数长度
            $uploadfile=implode(".",$filename);
            //$name1=$name.".Mcncc";

            $result = $this->upPicTo7niu($tmp,'com',$uploadfile,'aebo');
            return $result['key'];
        }
    }


    /**
     *上传图片至七牛云
     *$path 图片地址
     *$dirname 在七牛云上的文类名称
     *$filename 文件保存名称
     *$bucket 你的七牛云域名称
     */
    function upPicTo7niu($path,$dirname,$filename,$bucket){
        $contents = @file_get_contents($path);

        $storename = $dirname."/".$filename;
        $bu = $bucket.":".$storename;
        $accessKey = 'FWdy0abUDmanFns5ZArKEHjRl2e5hXeZVsLKSU7M';
        $secretKey = 'zuHKtuuL888TuuavvzbyWnGnytjW33zI1mrAql12';

        Qiniu_SetKeys($accessKey, $secretKey);
        $putPolicy = new \Qiniu_RS_PutPolicy($bu);
        $upToken = $putPolicy->Token(null);
        $putExtra = new \Qiniu_PutExtra();
        $putExtra->Crc32 = 1;
        list($ret, $err) = Qiniu_Put($upToken, $storename, $contents, $putExtra);
        return $ret;
    }
    //获取文件后缀名函数
    function fileext($filename)
    {
        return substr(strrchr($filename, '.'), 1);
    }

    function random($length)
    {
        $hash = 'CR-';
        $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
        $max = strlen($chars) - 1;
        mt_srand((double)microtime() * 1000000);
        for($i = 0; $i < $length; $i++)
        {
            $hash .= $chars[mt_rand(0, $max)];
        }
        return $hash;
    }

}